package com.ws.admin.post.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.ws.admin.post.entity.Post;
import com.ws.admin.post.entity.PostReply;
import com.ws.admin.post.mapper.PostMapper;
import com.ws.admin.post.service.IPostReplyService;
import com.ws.admin.post.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ws.common.utils.PageUtils;

/**
 * <p>
 * 论坛-帖子 服务实现类
 * </p>
 *
 * @author weifucheng
 * @since 2019-11-26
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    @Autowired
    private IPostReplyService replyService;

    @Override
    public Page<Post> list(Post post, Integer current, Integer size) {
        Page<Post> page= PageUtils.newPage(current, size);
        page.addOrder(OrderItem.desc("t.create_time"));
        page.setRecords(baseMapper.listByParam(page,post));
        return page;
    }

    @Override
    public Map<String, Object> readPostAndReply(Post post) {

        post=this.getById(post.getId());

        PostReply postReply=new PostReply();
        postReply.setPostId(post.getId());
        List<PostReply> postReplays=replyService.list(postReply,OrderItem.desc("t.create_time"));
        Map<String, Object> map=new HashMap<>();
        map.put("post",post);
        map.put("postReplays",postReplays);
        return map;
    }
}
